import { forgetPwd,getVercode } from '../../../../../common/api/index'
Page({
    data: {
		mobile: null,
		password: null,
		code: null,
		comfirmPwd: null,
		imgPath: null,
		uuid: null,
	},
    onShow() {
		// 自动获取验证码
		this.getVercodeFunc()
    },
	// 获取验证码
	getVercodeFunc(){
		getVercode().then(res => {
			if(res.code == 200){
				let imgPath = res.data.img.replace(/[\r\n]/g,"");
				this.setData({
					imgPath: imgPath,
					uuid:res.data.uuid
				})
			}
		})
	},
	// 输入改变
	onChange(e){
		let key = e.target.dataset.name;
		this.setData({
			[key]: e.detail
		})
	},
	// 确认修改密码
	comfirmChangePwd(){
		let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/;
		if(!this.data.mobile){
			wx.showToast({
				title: '请正确输入手机号',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.password || !reg.test(this.data.password)){
			wx.showToast({
				title: '请正确输入密码',
				icon: 'none',
				duration: 2000
			})
			return	
		}
		if(this.data.comfirmPwd != this.data.password){
			wx.showToast({
				title: '两次输入密码不一致',
				icon: 'none',
				duration: 2000
			})
			return
		}
		if(!this.data.code){
			wx.showToast({
				title: '请正确输入验证码',
				icon: 'none',
				duration: 2000
			})
			return
		}
		let data = {
			mobile: this.data.mobile,
			password: this.data.password,
			code: this.data.code,
			uuid: this.data.uuid,
		}
		forgetPwd(data).then(res =>{
			if(res.code == 200){
				wx.showToast({
					title: '密码修改成功',
					icon: 'none',
					duration: 2000
				})
				setTimeout(() => {
					this.setData({
						mobile: null,
						password: null,
						code: null,
						comfirmPwd: null
					})
					this.getVercodeFunc()
					wx.setStorageSync('isLogin',false);
					wx.reLaunch({
					  url: '/pages/component/mine/account/login/index',
					})
				},2000)
			}
		})
	},
	




    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})